﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using System.Collections;
using Contract.Domain;
using DevExpress.XtraTreeList.Nodes;

namespace CIIS
{
    public partial class OfficeControl : BaseUserControl
    {
        public OfficeControl()
        {
            InitializeComponent();
            InitOffice();
            LoadTree();
         //   this.officeTree.RootValue = -2;
            this.officeTree.OptionsBehavior.Editable = false;
            this.officeTree.OptionsView.ShowCheckBoxes = true;
        }
        public string Id
        {
            get;
            set;
        }
        private void LoadTree()
        {
            officeTree.DataSource = null;
            officeTree.DataSource = OfficeList;
            this.lookUpEdit1.Properties.DataSource = OfficeList;
            officeTree.OptionsView.ShowCheckBoxes = true;
            officeTree.OptionsView.ShowVertLines = true;
            officeTree.OptionsView.ShowHorzLines = true;
            officeTree.ParentFieldName = "ParentId";
            officeTree.KeyFieldName = "Id";
            officeTree.OptionsBehavior.PopulateServiceColumns = false;
          //  officeTree.OptionsBehavior.AllowIndeterminateCheckState = true;
         // officeTree.ExpandAll();
        }
        public IList<Contract.Domain.Office> OfficeList
        {
            get;
            set;
        }
        bool isReaload = false;
        private void InitOffice()
        {

            IList list = null;
            if (isReaload)
            {
                list = dao.ReloadDataList<Office>();
                isReaload = false;
            }
            else
            {
                list = dao.GetListFromCache<Office>();
            }
            OfficeList = new List<Contract.Domain.Office>();
            for (var i = 0; i < list.Count; i++)
            {
                var o = list[i] as Office;
                OfficeList.Add(o);
            }

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Id = string.Empty;
           this.textEdit1.Text=string.Empty;
           this.textEdit2.Text = string.Empty;
           this.textEdit3.Text = string.Empty;
           lookUpEdit1.SelectedText = "";
        }

     

        private void officeTree_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            var o = officeTree.GetDataRecordByNode(e.Node) as Contract.Domain.Office;
          this.Id = o.Id;
          lookUpEdit1.EditValue = o.ParentId;
          textEdit3.Text = o.Seq.HasValue?o.Seq.Value.ToString():"";
          textEdit1.Text = o.Name;
          textEdit2.Text = o.BH;
        //  SetCheckedChildNodes(e.Node, e.Node.CheckState);
          SetCheckedParentNodes(e.Node, e.Node.CheckState);
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Contract.Domain.Office o = new Office();
            o.Id = this.Id;
            o.Name = this.textEdit1.Text;
            o.BH = textEdit2.Text;
            var m = lookUpEdit1.GetSelectedDataRow() as Office;
            if(m!=null)
            {
                if (m.Id == o.Id)
                {
                    MessageBox.Show("上级单位不能等于本身！");
                    return;
                }
                else
                {
                    o.ParentId = m.Id;
                }
            }
            if (!string.IsNullOrEmpty(textEdit3.Text))
            {
                int a = 0;
                if (int.TryParse(textEdit3.Text, out a))
                {
                    o.Seq = a;
                }
                else
                {
                    MessageBox.Show("序号必须是数值！");
                    return;
                }
                
            }
            if (string.IsNullOrEmpty(o.Id))
            {
                o.State.MarkNew();
            }
            else
            {
                o.State.MarkDirty();
            }
            o = DataUtils.Instance.Dao.SaveOrUpdate(o) as Office;
            Id = o.Id;
            isReaload = true;
            InitOffice();
            officeTree.DataSource = OfficeList;
            lookUpEdit1.Properties.DataSource = OfficeList;

        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Id))
            {
                var o = new Contract.Domain.Office()
                {
                    Id = Id
                };
                o.State.MarkDeleted();
                DataUtils.Instance.Dao.SaveOrUpdate(o);
                isReaload = true;
                InitOffice();
                officeTree.DataSource = OfficeList;
                lookUpEdit1.Properties.DataSource = OfficeList;
                Id = string.Empty;
                this.textEdit1.Text = string.Empty;
                this.textEdit2.Text = string.Empty;
                this.textEdit3.Text = string.Empty;
                lookUpEdit1.SelectedText = "";
            }
        }

        private void officeTree_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
        }
        private void SetCheckedParentNodes(TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool b = false;
                CheckState state;
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
                {
                    state = (CheckState)node.ParentNode.Nodes[i].CheckState;
                    if (!check.Equals(state))
                    {
                        b = !b;
                        break;
                    }
                }
                node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }
      
    }
}
